package org.base.oj.service.impl;

import org.base.oj.domain.LoginUser;
import org.base.oj.domain.User;
import org.base.oj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * security框架需要实现UserDetailsService接口从数据库调用用户的信息
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private UserService userService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userService.getByUserName(username);
        if (user == null) throw new UsernameNotFoundException("用户名不存在");
        if (user.getStatus() == 1) throw new UsernameNotFoundException("用户被禁用");
        return new LoginUser(user);
    }
}
